package xyz.xiangyushui.controller.membership.profile;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xyz.xiangyushui.dao.user.member.MembershipProfileDao;
import xyz.xiangyushui.javaBean.membership.MembershipProfile;

/**
 * Servlet implementation class UpdateProfile
 */
@WebServlet("/member/profile/update")
public class UpdateProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MembershipProfileDao mpd;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateProfile() {
        super();
        mpd=new MembershipProfileDao();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String gender=request.getParameter("gender");
		String age=request.getParameter("age");
		String address=request.getParameter("address");
		System.out.println(gender+" "+ age+" "+address);
		try{
			int ageInt=Integer.parseInt(age);
			MembershipProfile mp=new MembershipProfile();
			mp.setAddress(address);
			mp.setAge(ageInt);
			mp.setGender(gender);
			mp.setUserName((String)request.getSession(false).getAttribute("userName"));
			mpd.update(mp);
		}catch(NumberFormatException e){
			request.setAttribute("failedMsg", "Invalid input age!");	
		}
		request.getRequestDispatcher("/member/profile").forward(request, response);
	}

}
